﻿using ECommerce.Web;
using ECommerce.Web.NHibernate;
using ECommerce.Web.NHibernate.ConfigurationBuilders;
using ECommerce.Web.NHibernate.Sessions;
using Machine.Specifications;
using NHibernate;
using NHibernateCfg = NHibernate.Cfg;

namespace ECommerce.IntegrationSpecs
{
    public abstract class InMemoryDatabaseContext
    {
        private static ISession session;

        protected static ISession Session
        {
            get { return session; }
        }

        Establish context = () =>
            {
                Bootstrapper.ConfigureServiceLocator();

                var configurationBuilder = new DefaultConfigurationBuilder();

                NHibernateCfg.Configuration configuration = configurationBuilder.Build(PersistenceOptions.InMemory());

                Bootstrapper.ConfigureSessionSource(configuration);

                session = SessionSource.Current.GetSession();

                var exporter = new SchemaExporter(configuration);

                exporter.Export(session.Connection);
            };

        Cleanup cleanup = () =>
            {
                SessionSource.Current.CloseSession();
            };
    }
}
